/*
 * @Author: XingWei 
 * @Date: 2021-07-23 10:15:43 
 * @Last Modified by: XingWei
 * @Last Modified time: 2021-12-13 19:55:01
 * 
 * 自定义AppBar
 */

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_demo/core/common/values/gaps.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

//PreferredSizeWidget 自定义字体大小
class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
  const MyAppBar(
      {Key? key,
      this.backgroundColor,
      this.foregroundColor,
      this.title = '',
      this.centerTitle = '',
      this.backImg = 'assets/images/dij.png',
      this.onPressed,
      this.isBack = true})
      : super(key: key);

  final Color? backgroundColor;
  final Color? foregroundColor;
  final String title;
  final String centerTitle;
  final String backImg;
  final VoidCallback? onPressed;
  final bool isBack;

  @override
  Widget build(BuildContext context) {
    final _backgroundColor = backgroundColor ?? Get.theme.cardColor;

    final _overlayStyle =
        ThemeData.estimateBrightnessForColor(_backgroundColor) ==
                Brightness.dark
            ? SystemUiOverlayStyle.light
            : SystemUiOverlayStyle.dark;

    final _foregroundColor = foregroundColor ??
        (Get.isDarkMode ? const Color(0xFFFFFFFF).withOpacity(0.9) : Colors.black);

    //回退按钮
    final back = isBack
        ? IconButton(
            onPressed: () async {
              FocusManager.instance.primaryFocus?.unfocus();
              if (onPressed != null) {
                onPressed!.call();
              } else {
                Get.back();
              }
            },
            // tooltip: 'Back',
            padding: const EdgeInsets.only(left: 12.0),
            icon: Image.asset(
              backImg,
              color: _foregroundColor,
              width: 25.w,
              height: 25.w,
            ),
          )
        : Gaps.empty;

    //Semantics 语义，作用：完全可选(这意味着你可以完全不关心这个控件，但这并不推荐)，
    //例如主要由视障人士使用
    //它会描述应用程序而无需查看屏幕
    final Widget titleWidget = Semantics(
      namesRoute: true,
      //这是个头部的部件
      header: true,
      child: Container(
        alignment:
            centerTitle.isEmpty ? Alignment.centerLeft : Alignment.center,
        width: double.infinity,
        //horizontal指left和right。 左右margin 44
        margin: EdgeInsets.symmetric(horizontal: 44.w),
        child: Text(
          title.isEmpty ? centerTitle : title,
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
          style: TextStyle(
              color: _foregroundColor,
              fontSize: 16.sp,
              fontWeight: FontWeight.w600),
        ),
      ),
    );

    // final Widget line = Positioned(
    //     top: 12,
    //     child: );

    //AnnotatedRegion 修改状态栏颜色
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: _overlayStyle,
      child: Material(
        color: _backgroundColor,
        child: SafeArea(
          child: Stack(
            alignment: Alignment.centerLeft,
            children: <Widget>[
              //标题栏
              titleWidget,
              //回退按钮
              back,
              // line,
            ],
          ),
        ),
      ),
    );
  }

  @override
  Size get preferredSize => Size.fromHeight(44.sp);
}
